﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Reflection;
using DevExpress.XtraSplashScreen;

namespace Baubixinh_client
{
    public partial class frmExport : DevExpress.XtraEditors.XtraForm
    {
        public frmExport()
        {
            InitializeComponent();
        }

        private void btnWholeSale_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                try
                {
                    SplashScreenManager.ShowForm(this, typeof(frmWaiting), true, true, false);
                    frmExportWholeSales frmNewImport = new frmExportWholeSales();

                    //check
                    foreach (Form form in Application.OpenForms)
                    {
                        if (form.GetType() == frmNewImport.GetType())
                        {
                            frmNewImport.Activate();
                            return;
                        }
                    }

                    frmNewImport.MdiParent = this.MdiParent;
                    frmNewImport.Show();
                }
                catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
                finally
                {
                    SplashScreenManager.CloseForm(false);
                }
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
        }

        private void btnRetail_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                try
                {
                    SplashScreenManager.ShowForm(this, typeof(frmWaiting), true, true, false);
                    frmExportRetail frmNewImport = new frmExportRetail();

                    //check
                    foreach (Form form in Application.OpenForms)
                    {
                        if (form.GetType() == frmNewImport.GetType())
                        {
                            frmNewImport.Activate();
                            return;
                        }
                    }

                    frmNewImport.MdiParent = this.MdiParent;
                    frmNewImport.Show();
                }
                catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
                finally
                {
                    SplashScreenManager.CloseForm(false);
                }
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
        }

        private void btnExportExcel_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                try
                {
                    frmExportFilter frmFilter = new frmExportFilter();
                    frmFilter.ShowDialog();
                    if (!frmFilter.FilterSelected) return;

                    SplashScreenManager.ShowForm(this, typeof(frmWaiting), true, true, false);

                    clsDbAccess.stExportFilter stFilter = new clsDbAccess.stExportFilter();
                    stFilter.intStoreID = frmFilter.StoreID;
                    stFilter.intStaffID = frmFilter.StaffID;
                    stFilter.dtDateFrom = frmFilter.DateFrom;
                    stFilter.dtDateTo = frmFilter.DateTo;
                    
                    frmExcel frmNewExcel = new frmExcel();
                    frmNewExcel.fncExport(clsDbAccess.fncGetDonHangBaoCao(Program.gobjDb, stFilter));
                    frmNewExcel.MdiParent = this.MdiParent;
                    frmNewExcel.Show();
                }
                catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
                finally
                {
                    SplashScreenManager.CloseForm(false);
                }
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
        }

        private void frmExport_Load(object sender, EventArgs e)
        {
            try
            {
                DataTable objExport;
                DataTable objDoiTuong;

                objExport = clsDbAccess.fncGetDonHang(Program.gobjDb, new clsDbAccess.stExportFilter());
                objDoiTuong = new DataTable();
                objDoiTuong.Columns.Add("value");
                objDoiTuong.Columns.Add("name");
                objDoiTuong.Rows.Add(new object[] { 1, "Khách lẻ" });
                objDoiTuong.Rows.Add(new object[] { 2, "Đại lý cấp 1" });
                objDoiTuong.Rows.Add(new object[] { 3, "Đại lý cấp 2" });
                objDoiTuong.Rows.Add(new object[] { 4, "Chuyển kho" });

                grdControlExport.DataSource = objExport;
                repositoryItemLookUpEdit1.DataSource = objDoiTuong;

                dgvBanHang.BestFitColumns();
                

                Program.gevnApplication.evnDonHangUpdate += gevnApplication_evnDonHangUpdate;

                grdControlExport.Focus();
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
        }

        void gevnApplication_evnDonHangUpdate()
        {
            try
            {
                DataTable objExport;

                objExport = clsDbAccess.fncGetDonHang(Program.gobjDb, new clsDbAccess.stExportFilter());

                grdControlExport.DataSource = objExport;
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
        }

        private void grdControlExport_Click(object sender, EventArgs e)
        {
            try
            {
                int intRow = dgvBanHang.FocusedRowHandle;
                if (intRow < 0) return;

                txtStoreName.Text = clsCommon.fncCnvNullToString(dgvBanHang.GetRowCellValue(intRow, colTen_khach));
                txtCustomer.Text = clsCommon.fncCnvNullToString(dgvBanHang.GetRowCellValue(intRow, colTenkhohang));
                txtPhone.Text = clsCommon.fncCnvNullToString(dgvBanHang.GetRowCellValue(intRow, colSdt));
                txtEmail.Text = clsCommon.fncCnvNullToString(dgvBanHang.GetRowCellValue(intRow, colEmail));
                txtSeller1.Text = clsCommon.fncCnvNullToString(dgvBanHang.GetRowCellValue(intRow, colnguoi_ban1_ten));
                txtSeller1.Text = clsCommon.fncCnvNullToString(dgvBanHang.GetRowCellValue(intRow, colnguoi_ban2_ten));

                txtDate.EditValue = clsCommon.fncCnvNullToDatetime(dgvBanHang.GetRowCellValue(intRow, colNgay_ban));
                txtCustomerType.Text = clsCommon.fncCnvNullToString(dgvBanHang.GetRowCellDisplayText(intRow, colLoai_khach));
                txtShipping.Text = clsCommon.fncCnvNullToString(dgvBanHang.GetRowCellValue(intRow, colVan_chuyen));
                txtTotal.Text = clsCommon.fncCnvNullToString(dgvBanHang.GetRowCellValue(intRow, colTong_cong));
                txtNote.Text = clsCommon.fncCnvNullToString(dgvBanHang.GetRowCellValue(intRow, colGhi_chu));

                DataTable objData = clsDbAccess.fncGetDonHangChiTiet(Program.gobjDb, clsCommon.fncCnvNullToInt(dgvBanHang.GetRowCellValue(intRow, colDon_hang_id)));
                grdControlProduct.DataSource = objData;
                dgvProduct.BestFitColumns();

                DataTable objTraLai = clsDbAccess.fncGetDonHangTraLai(Program.gobjDb, clsCommon.fncCnvNullToInt(dgvBanHang.GetRowCellValue(intRow, colDon_hang_id)));
                grdControlTraHang.DataSource = objTraLai;
                dgvTraHang.BestFitColumns();

                txtTotalReturn.Text = "0";
                btnReturn.Enabled = false;
                if (objTraLai == null) btnReturn.Enabled = true;
                
                //tinh tong tien tra lai
                if (objTraLai != null)
                {
                    int intTotal = 0;
                    int intAmount;

                    for (int i = 0; i <= dgvTraHang.RowCount - 1; i++)
                    {
                        intAmount = clsCommon.fncCnvNullToInt(dgvTraHang.GetRowCellValue(i, colThanh_tien2));
                        intTotal += intAmount;
                    }

                    txtTotalReturn.Text = intTotal.ToString();
                }
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
        }

        private void btnReturn_Click(object sender, EventArgs e)
        {
            try
            {
                DataTable objData = (DataTable)grdControlProduct.DataSource;

                if (objData == null) return;
                if (objData.Rows.Count == 0) return;

                int intDonHangID;
                int intKhohang;

                intDonHangID = clsCommon.fncCnvNullToInt(dgvBanHang.GetFocusedRowCellValue(colDon_hang_id));
                intKhohang  = clsCommon.fncCnvNullToInt(dgvBanHang.GetFocusedRowCellValue(colKho_hang_id));

                frmExportReturn frmReturn = new frmExportReturn(objData, intDonHangID, intKhohang);
                frmReturn.ShowDialog();
                if (frmReturn.DaTraLai) grdControlExport_Click(null, null);
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
        }
    }
}